<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="充值金额(元)">
        <el-input class="w500" v-model="amount" placeholder="例如：100"></el-input>
      </el-form-item>
      <el-form-item label="原因" prop="remark">
        <el-input type="textarea" class="w500" v-model="form.remark" autosize></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('form')">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { mapState } from 'vuex'
  import notify from '@/tools/notify'

  export default {
    data: function () {
      return {
        form: {
          user_id: '',
          money: '',
          user_info: '',
          opposite_info: '',
          remark: ''
        },
        amount: 0,
        rules: {
          remark: [
            {required: true, message: '请输入充值原因', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      ...mapState({
        pdata: state => state.fund.saveData,
        userPdata: state => state.user.info
      })
    },
    methods: {
      onSubmit (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.form.user_id = this.pdata.user_id
            this.form.user_info = this.pdata.user_info
            this.form.opposite_info = this.userPdata
            this.form.money = Number(this.amount) * 100
            this.$store.dispatch('fund/fundRecharge', this.form).then(res => {
              notify.success(res.msg)
              this.$router.push('/assets/fund')
            })
          }
        })
      },
      goBack () {
        this.$router.go(-1)
      }
    }
  }
</script>
